package com.arch.dev.client;

import java.util.List;
import java.util.Set;

import com.arch.dev.client.view.ui.BudgetToolBar;
import com.arch.dev.client.vo.CatagoryVO;
import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.SimplePager.TextLocation;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.MultiSelectionModel;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SelectionChangeEvent.Handler;
import com.google.gwt.view.client.SelectionModel;

public class CatagoryTable extends Composite {
	private static CatagoryTableUiBinder uiBinder = GWT

	.create(CatagoryTableUiBinder.class);

	interface CatagoryTableUiBinder extends UiBinder<Widget, CatagoryTable> {

	}

	@UiField(provided = true)
	CellTable<CatagoryVO> cellTable;

	@UiField(provided = true)
	SimplePager pager;

	@UiField(provided = true)
	BudgetToolBar budgetToolBar;

	Widget view;

	ListDataProvider<CatagoryVO> dataProvider;

	public CatagoryTable() {

		onInitialize();

		initWidget(uiBinder.createAndBindUi(this));

	}

	public CatagoryTable(Widget view) {

		this();

		this.view = view;

	}

	SelectionModel<CatagoryVO> selectionModel;

	public void onInitialize() {

		budgetToolBar = new BudgetToolBar();
		budgetToolBar.removeAllWidgets();
	

		cellTable = new CellTable<CatagoryVO>();

		cellTable.setWidth("100%", true);

		// Add a selection model so we can select cells.

		selectionModel = new MultiSelectionModel<CatagoryVO>(
				CatagoryVO.KEY_PROVIDER);

		cellTable
				.setSelectionModel(selectionModel, DefaultSelectionEventManager
						.<CatagoryVO> createCheckboxManager());

		// Create a Pager to control the table.

		SimplePager.Resources pagerResources = GWT

		.create(SimplePager.Resources.class);

		pager = new SimplePager(TextLocation.CENTER, pagerResources, false, 0,

		true);

		pager.setDisplay(cellTable);

		// Initialize the columns.

		initTableColumns();

		dataProvider = new ListDataProvider<CatagoryVO>();

		// Connect the table to the data provider.

		dataProvider.addDataDisplay(cellTable);

	}

	/**
	 * 
	 * Add the columns to the table.
	 */

	private void initTableColumns() {

		// Create name column.

		TextColumn<CatagoryVO> nameColumn = new TextColumn<CatagoryVO>() {

			@Override
			public String getValue(CatagoryVO contact) {

				return contact.getName();

			}

		};

		// Make the name column sortable.

		nameColumn.setSortable(true);

		
		Column<CatagoryVO, Boolean> checkColumn = new Column<CatagoryVO, Boolean>(
				new CheckboxCell(true, false)) {

			@Override
			public Boolean getValue(CatagoryVO object) {

				return selectionModel.isSelected(object);

			}

		};

		cellTable.setColumnWidth(checkColumn, 100, Unit.PX);

		cellTable.addColumn(checkColumn,
				SafeHtmlUtils.fromSafeConstant("<br/>"));

		cellTable.addColumn(nameColumn, "Description");

	

	}

	public void populateflexTable(List<CatagoryVO> catagoryVOs) {

		List<CatagoryVO> list = dataProvider.getList();

		list.clear();

		for (CatagoryVO catagoryVO : catagoryVOs) {

			list.add(catagoryVO);

		}

		cellTable.setPageSize(10);

	}

	class BudgetSelectionChangeHandler implements Handler {

		public void onSelectionChange(SelectionChangeEvent event) {

			MultiSelectionModel<CatagoryVO> selectionModel = (MultiSelectionModel<CatagoryVO>) event
					.getSource();

			Set<CatagoryVO> selected = selectionModel.getSelectedSet();

		}

	}

	public MultiSelectionModel<CatagoryVO> getMultiSelectionModel() {
		return (MultiSelectionModel<CatagoryVO>) selectionModel;
	}

	public void addToolBarWidget(Widget widget) {
		budgetToolBar.addToolWidget(widget);
	}

	/*
	 * public void addDeleteButtonClickHandler(ClickHandler handler) {
	 * //deleteButton.addClickHandler(handler);
	 * deleteImageButton.addClickHandler(handler); }
	 */
}
